<template>
  <div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
      <el-form-item label="网站名称" prop="nseName">
         <el-input v-model="dataForm.nseName" placeholder="网站名称"></el-input>
      </el-form-item>
      <el-form-item label="网站网址" prop="nseUrl">
         <el-input v-model="dataForm.nseUrl" placeholder="网站网址"></el-input>
      </el-form-item>
      <el-form-item label="网站Logo">
        <el-row>
          <el-col :span="18">
            <el-input v-model="dataForm.nseLogoUrl" v-bind:readonly="true" placeholder="网站Logo"></el-input>
          </el-col>
          <el-col :span="6">
            <el-upload
              class="upload-demo"
              :action="url"
              :before-upload="beforeUploadHandle"
              :on-success="successHandle"
              :multiple="false"
              :headers="myHeaders"
              :file-list="fileList"
              :on-remove="onRemove"
              style="text-align: center;">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-col>
        </el-row>
       </el-form-item>
       <el-form-item label="联系QQ" prop="contactQq">
          <el-input v-model="dataForm.contactQq" placeholder="联系QQ"></el-input>
       </el-form-item>
       <el-form-item label="联系邮箱" prop="contactEmail">
          <el-input v-model="dataForm.contactEmail" placeholder="联系邮箱"></el-input>
       </el-form-item>
       <el-form-item label="ICP备案号" prop="icp">
          <el-input v-model="dataForm.icp" placeholder="ICP备案号"></el-input>
       </el-form-item>
       <el-form-item label="SEO标题" prop="seoTitle">
          <el-input v-model="dataForm.seoTitle" placeholder="SEO标题"></el-input>
       </el-form-item>
       <el-form-item label="SEO关键词" prop="seoKeyWords">
          <el-input v-model="dataForm.seoKeyWords" placeholder="SEO关键词"></el-input>
       </el-form-item>
       <el-form-item label="SEO描述" prop="seoRemark">
          <el-input type="textarea"
            :autosize="{ minRows: 4, maxRows: 8}"
            v-model="dataForm.seoRemark"
            placeholder="SEO描述"></el-input>
       </el-form-item>
       <el-form-item label="网站底部信息" prop="netBottomInfo">
          <el-input type="textarea"
            :autosize="{ minRows: 4, maxRows: 8}"
            v-model="dataForm.netBottomInfo"
            placeholder="网站底部信息"></el-input>
       </el-form-item>
       <el-form-item label="交流论坛网址" prop="forumUrl">
          <el-input v-model="dataForm.forumUrl" placeholder="交流论坛网址"></el-input>
       </el-form-item>
       <el-form-item label="商家中心LOGO图">
         <el-row>
           <el-col :span="18">
             <el-input
              v-model="dataForm.businessCenterLogoUrl"
              v-bind:readonly="true"
              placeholder="商家中心LOGO图"></el-input>
           </el-col>
           <el-col :span="6">
             <el-upload
               class="upload-demo"
               :action="busurl"
               :before-upload="beforeBusUploadHandle"
               :on-success="successBusHandle"
               :multiple="false"
               :headers="myHeaders"
               :file-list="busfileList"
               :on-remove="onBusRemove"
               style="text-align: center;">
               <el-button size="small" type="primary">点击上传</el-button>
             </el-upload>
           </el-col>
         </el-row>
        </el-form-item>
        <el-form-item label="用户注册协议" prop="userRegistrationAgreement">
           <el-input type="textarea"
             :autosize="{ minRows: 8, maxRows: 16}"
             v-model="dataForm.userRegistrationAgreement"
             placeholder="用户注册协议"></el-input>
        </el-form-item>
        <el-form-item label="客服电话" prop="serviceTelNumber">
           <el-input v-model="dataForm.serviceTelNumber" placeholder="客服电话"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="isAuth('network:settings:save') || isAuth('network:settings:update')" :disabled="submitVisible" @click="dataFormSubmit()">提交</el-button>
    </span>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        busvisible: false,
        fileList: [],
        busfileList: [],
        url: '',
        busurl: '',
        num: 0,
        busnum: 0,
        successNum: 0,
        bussuccessNum: 0,
        dataForm: {
          nseId: '',
          nseName: '',
          nseUrl: '',
          nseLogoUrl: '',
          contactQq: '',
          contactEmail: '',
          icp: '',
          seoTitle: '',
          seoKeyWords: '',
          seoRemark: '',
          netBottomInfo: '',
          forumUrl: '',
          businessCenterLogoUrl: '',
          userRegistrationAgreement: '',
          serviceTelNumber: ''
        },
        files: [],
        busfiles: [],
        myHeaders: {
          token: this.$cookie.get('token')
        },
        dataRule: {
          nseName: [
            { required: true, message: '网站名称不能为空', trigger: 'blur' }
          ],
          nseUrl: [
            { required: true, message: '网站地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    activated () {
      this.init()
    },
    methods: {
      beforeUploadHandle (file) {
        this.num++
      },
      beforeBusUploadHandle (file) {
        this.busnum++
      },
      onBusRemove (file, fileList) {
        for (var i = 0; i < this.busfiles.length; i++) {
          if (file.name === this.busfiles[i].fileName) {
            this.busfiles.splice(i, 1)
          }
        }
      },
      onRemove (file, fileList) {
        for (var i = 0; i < this.files.length; i++) {
          if (file.name === this.files[i].fileName) {
            this.files.splice(i, 1)
          }
        }
      },
      successBusHandle (response, file, fileList) {
        this.busfileList = fileList
        this.bussuccessNum++
        if (response && response.code === 0) {
          if (this.busnum === this.bussuccessNum) {
            this.busfiles.push({'fileName': file.name, 'url': response.url})
            this.dataForm.businessCenterLogoUrl = response.url
            this.$confirm('操作成功, 是否继续操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(() => {
              this.busvisible = false
            })
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.fileList = fileList
        this.successNum++
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            this.files.push({'fileName': file.name, 'url': response.url})
            this.dataForm.nseLogoUrl = response.url
            this.$confirm('操作成功, 是否继续操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(() => {
              this.visible = false
            })
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      init () {
        this.submitVisible = false
        this.$refs['dataForm'].resetFields()
        this.fileList = []
        this.busfileList = []
        this.url = this.$http.adornUrl(`/pig-file/center/file/upload`)
        this.busurl = this.$http.adornUrl(`/pig-file/center/file/upload`)
        this.$http({
          url: this.$http.adornUrl('/pig-center-biz/network/settings/info'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.nseId = data.settings.nseId
            this.dataForm.nseName = data.settings.nseName
            this.dataForm.nseUrl = data.settings.nseUrl
            this.dataForm.nseLogoUrl = data.settings.nseLogoUrl
            this.dataForm.contactQq = data.settings.contactQq
            this.dataForm.contactEmail = data.settings.contactEmail
            this.dataForm.icp = data.settings.icp
            this.dataForm.seoTitle = data.settings.seoTitle
            this.dataForm.seoKeyWords = data.settings.seoKeyWords
            this.dataForm.seoRemark = data.settings.seoRemark
            this.dataForm.netBottomInfo = data.settings.netBottomInfo
            this.dataForm.forumUrl = data.settings.forumUrl
            this.dataForm.businessCenterLogoUrl = data.settings.businessCenterLogoUrl
            this.dataForm.userRegistrationAgreement = data.settings.userRegistrationAgreement
            this.dataForm.serviceTelNumber = data.settings.serviceTelNumber
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/network/settings/${!this.dataForm.nseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'nseId': this.dataForm.nseId || undefined,
                'nseName': this.dataForm.nseName,
                'nseUrl': this.dataForm.nseUrl,
                'nseLogoUrl': this.dataForm.nseLogoUrl,
                'contactQq': this.dataForm.contactQq,
                'contactEmail': this.dataForm.contactEmail,
                'icp': this.dataForm.icp,
                'seoTitle': this.dataForm.seoTitle,
                'seoKeyWords': this.dataForm.seoKeyWords,
                'seoRemark': this.dataForm.seoRemark,
                'netBottomInfo': this.dataForm.netBottomInfo,
                'forumUrl': this.dataForm.forumUrl,
                'businessCenterLogoUrl': this.dataForm.businessCenterLogoUrl,
                'userRegistrationAgreement': this.dataForm.userRegistrationAgreement,
                'serviceTelNumber': this.dataForm.serviceTelNumber
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.submitVisible = false
                    this.init()
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style>
</style>
